#!/usr/bin/env python
#-*- coding:utf-8 -*-

"""
Basic image loading and display example.
Demonstrates checking for pressed keys and how to trigger a callback.
"""

from tools import *
from visualise import *

def main():
    image = load_image("../media/hi_gray.png")
    print 'can you see the "hi" in the data?'
    print image

    # display some images

    directory = "../media"

    for filename in os.listdir(directory):
        filename = os.path.join(directory, filename)

        if not filename.endswith(".jpg"):
            continue

        image = load_image(filename)

        viewer = Viewer(name=filename, show_fps=True)
        viewer.update_image(image)

        def callback(filename=filename):
            # important! Python gotcha:
            # use x=x when defining a callback's signature in a loop
            # or you'll end up just using the last values
            # i.e. if we used "def callback(filename)" then all
            # callbacks would have printed the last value of filename
            print "you are looking at %s" % filename
        viewer.set_callback(pyglet.window.key.SPACE, callback)

    while 1:
        symbol, viewer = Viewer.draw_all()
        if symbol is not None:
            # http://www.pyglet.org/doc/programming_guide/keyboard_events.html
            print "you pressed %s on viewer %s" % (pyglet.window.key.symbol_string(symbol), viewer)
        if not Viewer.instances:
            break

if __name__ == '__main__':
    main()


